package com.machine.filling.function.main.orderInfoFg;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.machine.filling.R;
import com.machine.filling.db.dao.ClientInfoDao;
import com.machine.filling.db.dao.GunInfoDao;
import com.machine.filling.function.main.base.BaseFgFragment;
import com.machine.filling.function.main.topFg.TopFgFragment;
import com.machine.filling.net.ws.response.WsResPreOrderInfo;
import com.machine.filling.util.Common;
import com.machine.filling.util.StringUtil;

/**
 * Created by Administrator on 2018/2/1 0001.
 * 预约订单
 */

public class OrderInfoFgFragment extends BaseFgFragment<OrderInfoFgContract.Presenter> implements OrderInfoFgContract.View, View.OnClickListener {

    private EditText et_order_num;
    private Button bt_search;
    private ImageView iv_back;
    private TextView tv_order_num;
    private TextView tv_order_price;
    private TextView tv_can_fueling;
    private TextView tv_back_time;

    private WsResPreOrderInfo mOrderBean;
    private String portNum;
    private RelativeLayout rl;
    private long BACK_TIME = Common.BACK_TIME;//返回主界面的倒计时s


    public OrderInfoFgFragment(String portNum) {
        this.portNum = portNum;
    }

    @Override
    public View initViews() {
        View view = LayoutInflater.from(mActivity).inflate(R.layout.fg_fg_orderinfo, null);
        et_order_num = (EditText) view.findViewById(R.id.et_input_order_num);
        rl = view.findViewById(R.id.rl);
        bt_search = (Button) view.findViewById(R.id.bt_search);
        iv_back = (ImageView) view.findViewById(R.id.iv_back);
        tv_order_num = (TextView) view.findViewById(R.id.tv_order_num);
        tv_order_price = (TextView) view.findViewById(R.id.tv_order_price);
        tv_can_fueling = (TextView) view.findViewById(R.id.tv_can_fueling);
        tv_back_time = (TextView) view.findViewById(R.id.tv_back_time);
        iv_back.setOnClickListener(this);
        bt_search.setOnClickListener(this);
        et_order_num.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.showSoftInput(et_order_num, InputMethodManager.SHOW_FORCED);
                    imm.hideSoftInputFromWindow(et_order_num.getWindowToken(), 0); //强制隐藏键盘
                }
            }
        });
        return view;
    }

    @Override
    public void initDatas() {
        et_order_num.setText("");
        et_order_num.setEnabled(true);
        mPresenter = new OrderInfoFgPresenter(mContext, this);
        BACK_TIME = Common.BACK_TIME;
        tv_back_time.setText(BACK_TIME + "s");
        handler.sendEmptyMessageDelayed(1001, 1 * 1000);//每隔1秒进行一次发消息更新界面
    }

    @Override
    public String getStationId() {
        return ClientInfoDao.getInstance().getModleByDeviceNum().getStationId();
    }

    @Override
    public String getGunId() {
        return GunInfoDao.getInstance().queryBytPortNum(portNum).getGunId();
    }

    @Override
    public String getPreOrderNum() {
        return et_order_num.getText().toString().trim();
    }

    /**
     * http获取预约订单信息
     *
     * @param orderBean
     */
    @Override
    public void showPreOrderInfo(WsResPreOrderInfo orderBean) {
        handler.removeMessages(1001);//取消之前所有循环,从新开始循环
        BACK_TIME = Common.BACK_TIME;
        handler.sendEmptyMessageDelayed(1001, 1 * 1000);//每隔1秒进行一次发消息更新界面
        showOrderNumListener.showOrderNum(orderBean.getOrderNum());
        mOrderBean = orderBean;
        tv_order_num.setText(orderBean.getOrderNum());
        tv_order_price.setText(orderBean.getAppPrice() + "元/升");
        // 发送命令,定量加油
        mPresenter.port_sendOrder_110A(portNum, mOrderBean);
        tv_can_fueling.setText("订单已锁定!\n可以开始抬枪加油！");

        et_order_num.setEnabled(false);
        bt_search.setEnabled(false);
        bt_search.setClickable(false);
    }

    @Override
    public void showPreOrderInfoError(String errorCode, String errorMsg) {
        tv_can_fueling.setText(/*errorCode + "," +*/ errorMsg);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_search:
                if (!StringUtil.isNullOrEmpty(et_order_num.getText().toString().trim())) {
                    tv_can_fueling.setText("订单识别中...");
                    mPresenter.getPreOrderInfo(portNum);
                } else {
                    tv_can_fueling.setText("请输入预约号码!");
                }
                break;
            case R.id.iv_back:
                //如果已经获取到订单编号,则走cancle
//                cancleOrder();
                BACK_TIME = 1;
                break;
        }
    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1001:
                    BACK_TIME--;
                    tv_back_time.setText(BACK_TIME + "s");
                    if (BACK_TIME <= 0) {
                        cancleOrder();
                    } else {
                        handler.sendEmptyMessageDelayed(1001, 1 * 1000);
                    }
                    break;
            }
        }

    };

    @Override
    public void onPause() {
        super.onPause();
        if (BACK_TIME >=1) {
            handler.removeMessages(1001);//取消之前所有循环
        }
    }

    /**
     * http解锁订单,串口取消定量
     */
    public void cancleOrder() {
        if (mOrderBean != null) {
            //取消定量命令
            mPresenter.port_sendCancleOrder_110B(portNum, mOrderBean);
        }
        //解锁订单
        mPresenter.http_unLockOrder(portNum);
        INextFragment.replaceNext(new Intent(), TopFgFragment.class.getName());
    }

    public void setShowOrderNumListener(ShowOrderNumListener showOrderNumListener) {
        this.showOrderNumListener = showOrderNumListener;
    }

    public ShowOrderNumListener showOrderNumListener;

    public interface ShowOrderNumListener {
        void showOrderNum(String orderNum);
    }
}
